# qirssi
# Copyright (c) 2009 Paulo Matias
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

use Irssi;
use Socket;
use POSIX;
use IO::Handle;

$::VERSION='0.0.1';
%::IRSSI = (
    authors => 'Paulo Matias',
    contact => 'archlinux-br.org#matias',
    name => 'qirssi',
    description => 'Client-side part of qirssi',
    url => 'http://matias.archlinux-br.org',
    license => 'ISC',
    changed => '2009-03-04'
    );


my $pub_notify_type = 'none';
my $priv_notify_type = 'none';

my $pub_msg_regex = '';
my $priv_nick_regex = '';

sub connect_remote {
    socket(REMOTE, PF_UNIX, SOCK_DGRAM, 0);
    connect(REMOTE, sockaddr_un(getenv("HOME")."/.irssi/qirssi.sock"));
    REMOTE->autoflush(1);
}

sub pub_msg {
    my ($server,$msg,$nick,$address,$target) = @_;
    if(($pub_notify_type ne 'none') && ($msg =~ $pub_msg_regex))
    {
        connect_remote();
        if($pub_notify_type =~ 'icon') {
            print REMOTE "icon on";      
        }
        if($pub_notify_type =~ 'balloon') {
            print REMOTE "show $target <$nick> $msg";
        }
        close(REMOTE);
    }
}

sub priv_msg {
    my ($server,$msg,$nick,$address) = @_;
    if(($priv_notify_type ne 'none') && ($nick =~ $priv_nick_regex))
    {
        connect_remote();
        if($priv_notify_type =~ 'icon') {
            print REMOTE "icon on";
        }
        if($priv_notify_type =~ 'balloon') {
            print REMOTE "show $nick $msg";
        }
        close(REMOTE);
    }
}

sub cmd_qirssi {
    my ($msg_type,$notify_type,$regex) = split(/\s+/, shift, 3);
    
    if(!$msg_type) {
        Irssi::active_win()->print("Usage: /qirssi pub|priv none|icon|balloon regex");
        Irssi::active_win()->print("Status:");
        Irssi::active_win()->print("  pub_msg alerts via \2$pub_notify_type\2 when message contains \2$pub_msg_regex\2.");
        Irssi::active_win()->print("  priv_msg alerts via \2$priv_notify_type\2 when nick contains \2$priv_nick_regex\2.");
        return;
    }
   
    if($msg_type eq 'pub') {
        $pub_notify_type = $notify_type;
        $pub_msg_regex = $regex;
    }
    elsif($msg_type eq 'priv') {
        $priv_notify_type = $notify_type;
        if($regex) {
            $priv_nick_regex = $regex;
        }
        else {
            $priv_nick_regex = '.+';
        }
    }
}

Irssi::signal_add_last('message public', \&pub_msg);
Irssi::signal_add_last('message private', \&priv_msg);
Irssi::command_bind('qirssi', \&cmd_qirssi);

